package com.maya.medicalwastes;

import android.app.Application;
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.inuker.bluetooth.library.BluetoothContext;
import com.maya.medicalwastes.entity.KeyBean;
import com.maya.medicalwastes.entity.SpConfig;
import com.maya.medicalwastes.entity.UserEntity;
import com.maya.medicalwastes.httpNet.interceptor.AddCookiesInterceptor;
import com.maya.medicalwastes.httpNet.interceptor.ReceivedCookiesInterceptor;
import com.maya.medicalwastes.httpNet.manage.OkHttpManager;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.util.ApiConfigConstant;
import com.maya.medicalwastes.util.PrefUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Interceptor;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-04 17
 * 描述：App
 */
public class App extends Application {
    /**
     * 当前用户信息
     */
    private static UserEntity userEntity;
    /**
     * 当前用户信息
     */
    private static List<KeyBean> mKeyBean;
    /**
     * 实例
     */
    private static App app;

    /**
     * @description 获取应用实例
     * @author gzl
     * @time 2017/4/28 10:47
     */
    public static App getInstance() {
        return app;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initRetrofit(this);
        BluetoothContext.set(this);
    }

    public static void initRetrofit(Context context) {
        RetrofitManager.getInstance().setBaseUrl(ApiConfigConstant.DEBUG_URL);
        AddCookiesInterceptor addCookiesInterceptor = AddCookiesInterceptor.getInstance(context, ApiConfigConstant.URL_HOST);
        ReceivedCookiesInterceptor receivedCookiesInterceptor = ReceivedCookiesInterceptor.getInstance(context, ApiConfigConstant.URL_HOST);
        List<Interceptor> list = new ArrayList<>();
        list.add(addCookiesInterceptor);
        list.add(receivedCookiesInterceptor);
        RetrofitManager.getInstance().build(OkHttpManager.getInstance(context).build(list).getClient());

    }

    public void setUserEntity(UserEntity userEntity) {
        this.userEntity = userEntity;
        //存入sp
        if (userEntity != null) {
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            String str = gson.toJson(userEntity, UserEntity.class);
            PrefUtils.putString(getApplicationContext(), SpConfig.USER_ENTITY, str);
        } else {
            PrefUtils.putString(getApplicationContext(), SpConfig.USER_ENTITY, "");
        }
    }
    public void setWareType(List<KeyBean> keyBean) {
        this.mKeyBean = keyBean;
        //存入sp
        if (mKeyBean != null) {
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            String str = gson.toJson(keyBean);
            PrefUtils.putString(getApplicationContext(), SpConfig.KEY_ENTITY, str);
        } else {
            PrefUtils.putString(getApplicationContext(), SpConfig.KEY_ENTITY, "");
        }
    }
    public List<KeyBean> getWareType() {
        List<KeyBean> datalist=new ArrayList<KeyBean>();
        //取sp
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String str = PrefUtils.getString(getApplicationContext(), SpConfig.KEY_ENTITY, "");
        if (!str.isEmpty()) {
             datalist=gson.fromJson(str, new TypeToken<List<KeyBean>>(){}.getType());
            return datalist;
//            try {
//                JsonArray array = new JsonParser().parse(str).getAsJsonArray();
//                for (JsonElement jsonElement : array) {
//                    datalist.add(gson.fromJson(jsonElement, KeyBean.class));
//                    return datalist;
//
//                }
//            } catch (Exception e) {
//            }

        }
        return datalist;


    }
    public UserEntity getUserEntity() {
        //取sp
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String str = PrefUtils.getString(getApplicationContext(), SpConfig.USER_ENTITY, "");
        if (!str.isEmpty()) {
            UserEntity entity = gson.fromJson(str, UserEntity.class);
            // String d = entity.getBirthday();
            return entity;
        }
        return userEntity;


    }


}
